﻿using System.Windows.Forms;

namespace ComputerVision.Common
{
    public class Utilities
    {
        public static bool IsFormOpen(Form form)
        {
            foreach (Form f in Application.OpenForms)
                if (f.Name.Equals(form.Name) && f.Text.Equals(form.Text))
                    return true;
            return false;
        }

        public static bool IsFormOpen(string formName)
        {
            foreach (Form f in Application.OpenForms)
                if (f.Name.Equals(formName))
                    return true;
            return false;
        }

        public static void ActiveForm(Form form)
        {
            foreach (Form f in Application.OpenForms)
                if (f.Name.Equals(form.Name) && f.Text.Equals(form.Text))
                {
                    f.Activate();
                    break;
                }
            form.Dispose();
        }

        public static void CloseOldForm(Form form)
        {
            foreach (Form f in Application.OpenForms)
                if (f.Name.Equals(form.Name) && f.Text.Equals(form.Text))
                {
                    f.Close();
                    break;
                }
        }

        public static void CloseOldFormByName(string formName)
        {
            foreach (Form f in Application.OpenForms)
                if (f.Name.Equals(formName))
                {
                    f.Close();
                    
                    //break;
                }
            System.GC.Collect();
        }

        public static void CloseAllOldForm()
        {
            int countF = Application.OpenForms.Count;
            Form form;

            for (int i = 0; i < countF; i++)
            {
                form = Application.OpenForms[i];

                if (!form.Name.Equals("frmMain") && !form.Name.Equals("frmLogin"))
                {
                    form.Close();
                    countF--;
                    //break;
                }
            }

            //foreach (Form f in Application.OpenForms)
            //    if (!f.Name.Equals("frmMain"))
            //    {
            //        f.Close();
                    
            //        //break;
            //    }
            //System.GC.Collect();
        }
    }
}